<?php

namespace app\models;

use yii;
use yii\base\Model;

class EntryForm extends Model
{
    public $name;
    public $email;

    public function rules()
    {
        return [
//            ['name','required','message'=>'请输入用户名'],
//            ['email','required','message'=>'请输入邮箱'],
//            ['email','email','message'=>'邮箱格式不正确'],
//            [['name','email'],'trim'],//去除首位空格

//            [['name','email'],'required'],
//            ['email','email'],

            ['name','required','message'=>'用户名不能为空'],
            ['name','validateName'],
            ['email','required','message'=>'请输入邮箱'],
            ['email','email','message'=>'邮箱格式不正确'],

        ];
    }

    public function attributeLabels()
    {
        return [
            'name' => '用户名',
            'email' => '邮箱',
        ];
    }

    public function validateName($attribute, $params)
    {
        //验证
//        if (!in_array($this->$attribute, ['USA', 'Web'])) {
//            $this->addError($attribute, 'The country must be either "USA" or "Web".');
//        }
        if($this->$attribute!='admin'){
            $this->addError($attribute, '用户名必须是admin.');
        }
    }
}